package userDao2

import (
	"gorm.io/gorm"
	"iristest/common/log"
	"iristest/dao"
	"iristest/do/user"
)

type IUserDao interface {
	SelectInfoByUsername(username, password string) (*user.UserInfo, error)
	SelectUserInfoByUserId(id int64) *user.UserInfo
	InsertUser(u *user.User) int64
	InsertUserInfo(u *user.UserInfo) int64
	UpdateUserInfoByUserId(u *user.UserInfo) bool
	DelUserAndInfoByIdForge(id int64) bool
	DelUserAndInfoById(id int64) bool
	SelectAllUser() *[]map[string]interface{}
}
type UserDao struct {
	db *gorm.DB
}

func New() IUserDao {
	return &UserDao{
		db: dao.Gorm,
	}
}

func (ud *UserDao) SelectInfoByUsername(username string, password string) (*user.UserInfo, error) {
	ui := &user.UserInfo{}
	err := ud.db.Model(ui).Joins("join user on user.id = user_info.user_id").
		Where("username=? AND password=?", username, password).First(ui).Error
	//err := ud.db.Select("id", "password", "status").Where("username=?", username).First(u).Error
	return ui, err
}

func (ud *UserDao) SelectUserInfoByUserId(id int64) *user.UserInfo {
	u := &user.UserInfo{}
	err := ud.db.Where("user_id=?", id).First(u).Error
	if err != nil {
		log.Err(err, "userid select info failed")
		return nil
	}
	if u == nil {
		return nil
	}
	return u
}

func (ud *UserDao) InsertUser(u *user.User) int64 {
	err := ud.db.Create(u).Error
	if err != nil {
		log.Err(err, "注册插入数据时发生错误")
		return 0
	}
	return (*u).Id
}

func (ud *UserDao) InsertUserInfo(u *user.UserInfo) int64 {
	err := ud.db.Create(u).Error
	if err != nil {
		log.Err(err, "注册插入数据时发生错误")
		return 0
	}
	return (*u).Id
}

func (ud *UserDao) UpdateUserInfoByUserId(u *user.UserInfo) bool {
	this := ud.db.Model(u).Where("user_id=?", (*u).UserId).Updates(*u)
	if this.Error != nil {
		log.Err(this.Error, "更新用户信息时数据库时出现错误")
		return false
	}
	return this.RowsAffected != 0
}

func (ud *UserDao) DelUserAndInfoByIdForge(id int64) bool {
	u := &user.User{
		Status: 4,
	}
	this1 := ud.db.Model(u).Where("id = ?", id).Updates(*u)
	if this1.Error != nil {
		log.Err(this1.Error, "伪删除用户时错误")
		return false
	}
	ui := &user.UserInfo{
		Status: 4,
	}
	this2 := ud.db.Model(ui).Where("user_id=?", id).Updates(*ui)
	if this2.Error != nil {
		log.Err(this2.Error, "伪删除用户信息时错误")
		return false
	}
	return this1.RowsAffected != 0 || this2.RowsAffected != 0
}

func (ud *UserDao) DelUserAndInfoById(id int64) bool {
	u := &user.User{
		Id: id,
	}
	ui := &user.UserInfo{}
	err := ud.db.Transaction(func(tx *gorm.DB) error {
		if err := ud.db.Delete(u).Error; err != nil {
			log.Err(err, "删除用户信息时错误")
			return err
		}
		if err := ud.db.Where("user_id=?", id).Delete(ui).Error; err != nil {
			log.Err(err, "删除用户信息时错误")
			return err
		}
		return nil
	})
	return err == nil
}

func (ud *UserDao) SelectAllUser() *[]map[string]interface{} {
	res := make([]map[string]any, 0)
	r := ud.db.Model(&user.User{}).Select("user.id,user.username,user.status,user_info.name,user_info.photo,user_info.sex,user_info.birthday,user_info.role,user_info.status as state").
		Joins("join user_info on user.id=user_info.user_id").Limit(10).Scan(&res)
	if r.Error != nil {
		log.Err(r.Error, "select all user failed")
		return nil
	}
	return &res
}
